package com.eshop.modules.wechat.rest;

import org.springframework.web.bind.annotation.DeleteMapping;
import java.io.Serializable;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.PostMapping;
import com.eshop.modules.aop.ForbidSubmit;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.validation.annotation.Validated;
import com.eshop.modules.mp.domain.WechatTemplate;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.data.domain.Pageable;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import io.swagger.annotations.ApiOperation;
import java.io.IOException;
import com.eshop.modules.mp.service.dto.WechatTemplateDto;
import com.eshop.modules.mp.service.dto.WechatTemplateQueryCriteria;
import javax.servlet.http.HttpServletResponse;
import com.eshop.dozer.service.IGenerator;
import com.eshop.modules.mp.service.WechatTemplateService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import io.swagger.annotations.Api;

@Api(tags = { "商城:微信模板管理" })
@RestController
@RequestMapping({ "/api/yxWechatTemplate" })
public class WechatTemplateController
{
    private final WechatTemplateService wechatTemplateService;
    private final IGenerator generator;
    
    @ApiOperation("导出数据")
    @GetMapping({ "/download" })
    @PreAuthorize("@el.check('admin','yxWechatTemplate:list')")
    public void download(final HttpServletResponse response, final WechatTemplateQueryCriteria criteria) throws IOException {
        this.wechatTemplateService.download(this.generator.convert(this.wechatTemplateService.queryAll(criteria), (Class)WechatTemplateDto.class), response);
    }
    
    @GetMapping
    @ApiOperation("查询微信模板消息")
    @PreAuthorize("@el.check('admin','yxWechatTemplate:list')")
    public ResponseEntity<Object> getYxWechatTemplates(final WechatTemplateQueryCriteria criteria, final Pageable pageable) {
        return new ResponseEntity(this.wechatTemplateService.queryAll(criteria, pageable), HttpStatus.OK);
    }
    
    @ForbidSubmit
    @PostMapping
    @ApiOperation("新增微信模板消息")
    @PreAuthorize("@el.check('admin','yxWechatTemplate:add')")
    public ResponseEntity<Object> create(@Validated @RequestBody final WechatTemplate resources) {
        return new ResponseEntity(this.wechatTemplateService.save(resources), HttpStatus.CREATED);
    }
    
    @ForbidSubmit
    @PutMapping
    @ApiOperation("修改微信模板消息")
    @PreAuthorize("@el.check('admin','yxWechatTemplate:edit')")
    public ResponseEntity<Object> update(@Validated @RequestBody final WechatTemplate resources) {
        this.wechatTemplateService.updateById(resources);
        return new ResponseEntity(HttpStatus.NO_CONTENT);
    }
    
    @ForbidSubmit
    @ApiOperation("删除微信模板消息")
    @PreAuthorize("@el.check('admin','yxWechatTemplate:del')")
    @DeleteMapping({ "/{id}" })
    public ResponseEntity<Object> deleteAll(@PathVariable final Integer id) {
        this.wechatTemplateService.removeById((Serializable)id);
        return new ResponseEntity(HttpStatus.OK);
    }
    
    public WechatTemplateController(final WechatTemplateService wechatTemplateService, final IGenerator generator) {
        this.wechatTemplateService = wechatTemplateService;
        this.generator = generator;
    }
}
